package com.salt.controller;

import com.salt.common.Result;
import com.salt.entity.MaterialQueryRequest;
import com.salt.service.TaobaoApiService;
import com.taobao.api.ApiException;
import com.taobao.api.response.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * 淘宝API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/taobao")
@RequiredArgsConstructor
public class TaobaoApiController {
    
    private final TaobaoApiService taobaoApiService;
    
    /**
     * 获取商品导览信息
     */
    @GetMapping("/material-guide")
    public Result<?> getMaterialGuide() {
        try {
            return Result.success(taobaoApiService.getMaterialGuide());
        } catch (ApiException e) {
            log.error("获取商品导览信息失败", e);
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/material1")
    public Result<?> getMaterialGuide1( @RequestParam(required = false)Long num) throws ApiException {
        AlibabaIdleAffiliateCpsIncomeDetailsQueryResponse alibabaIdleAffiliateCpsIncomeDetailsQueryResponse = taobaoApiService.queryCpsIncomeDetails(null,
                null,           // billState: 0-全部
                20l,
                num,
                1749744977000L,         // startTime
                null,
                null,         // startTime
                null
        );
        return Result.success(alibabaIdleAffiliateCpsIncomeDetailsQueryResponse);
    }


    @GetMapping("/material2")
    public Result<?> getMaterialGuide2( @RequestParam(required = false) String ids) throws Exception {
        String decodedItemId = java.net.URLDecoder.decode(ids, java.nio.charset.StandardCharsets.UTF_8.name());

        AlibabaIdleAffiliateCpsIncomeDetailsSignleQueryResponse alibabaIdleAffiliateCpsIncomeDetailsSignleQueryResponse = taobaoApiService.queryCpsIncomeDetailsResponse(Collections.singletonList(decodedItemId)
        );
        return Result.success(alibabaIdleAffiliateCpsIncomeDetailsSignleQueryResponse);
    }


    
    /**
     * 生成推广链接
     *
     * @param itemId 商品ID
     * @return 推广链接信息
     */
    @GetMapping("/promotion-link")
    public Result<?> generatePromotionLink(@RequestParam String itemId,@RequestParam Long materialType) {
        try {
            return Result.success(taobaoApiService.generatePromotionLink(itemId, materialType));
        } catch (Exception e) {
            log.error("生成推广链接失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询闲鱼联盟物料
     */
    @PostMapping("/materials")
    public Result<AlibabaIdleAffiliateMaterialQueryResponse> queryMaterials(
        @RequestBody MaterialQueryRequest request
    ) {
        try {
            return Result.success(taobaoApiService.queryMaterials(
                request.getMaterialType(),
                request.getPageSize(),
                request.getPageNum(),
                request.getTabName(),
                request.getItemPublisherTime(),
                request.getSellerCreditLevel(),
                request.getFilterLevel5Yxp(),
                request.getFilterYhb(),
                request.getSellerName(),
                request.getKeyword()
            ));
        } catch (Exception e) {
            log.error("查询闲鱼联盟物料失败", e);
            return Result.error(e.getMessage());
        }
    }
}
